package com.compass.doctor.ui.prescription.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.LayoutRxExamineItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseRecyclerViewAdapter
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.model.Diagnosis
import com.compass.framework.model.Drug
import com.compass.framework.model.TemplateDetails
import com.compass.framework.utils.DateUtils

/**
 * Description： 处方审核adapter
 * Date:2024/2/21 15:01
 */
class RxExamineAdapter : BaseRecyclerViewAdapter<TemplateDetails, LayoutRxExamineItemBinding>() {

    private  var onCustomBottomClick: OnCustomButtonClick?=null
    fun setBottomCallBack(onChatBottomClick: OnCustomButtonClick) {
        this.onCustomBottomClick = onChatBottomClick
    }
    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): LayoutRxExamineItemBinding {
        return LayoutRxExamineItemBinding.inflate(layoutInflater, parent, false)
    }

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<LayoutRxExamineItemBinding>,
        item: TemplateDetails?,
        position: Int
    ) {
        if (item == null) return
        holder.binding.apply {
            tvTime.text= DateUtils.conversionTime(item.createTime, DateUtils.FORMAT_TWO)

            item.hospitalPharmacistAuditTime?.let {
                tvExamineDate.text= DateUtils.conversionTime(it, DateUtils.FORMAT_TWO)
                llExamineDate.visible()
            } ?:kotlin.run {
                llExamineDate.gone()
            }

            tvDiagnosis.text=getDiagnosis(item.diagnosis)
            tvSymptom.text=getDrugs(item.drugs)
            tvPatientName.text=item.patientName
            tvDoctorName.text=item.doctorName
            tvStatus.text= DictionariesManager.getOrderStatus(item.status)
            tvDetails.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "details")
            }


//            when(item.status){
//                "-1"->{
//                    //作废
//                    llReason.visible()
//                    tvReasonName.text="作废原因"
//                    tvReason.text=item.cancelDescription
//                }
//                "-2"->{
//                    //拒绝
//                    tvReasonName.text="拒绝原因"
//                    llReason.visible()
//                    tvReason.text=item.cancelDescription
//                }
//                else->{
//                    llReason.gone()
//
//                }
//            }
        }

    }


    private fun getDiagnosis(list:MutableList<Diagnosis>?):String{
        list?.let {
            for (item in list){
                if (item.main) return item.name
            }
        }
        return ""
    }

    private fun getDrugs(list:MutableList<Drug>?):String{
        var listName=ArrayList<String?>()
        list?.let {
            for (item in list){
                listName.add(item.name)
            }
        }
        return  listName.joinToString(separator ="/")
    }
}